package com.refresh;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.handmark.pulltorefresh.library.R;


/**
 * 这个类封装了下拉刷新的布局
 */
public class FooterLoadingLayout extends LoadingLayout {
    /**
     * 旋转动画时间
     */
    private static final int ROTATE_ANIM_DURATION = 150;
    /**
     * 进度条
     */
    private SimpleDraweeView mProgressBar;
    /** 显示的文本 */
//    private TextView mHintView;
    /**
     * 箭头图片
     */
    private ImageView mArrowImageView;
    /**
     * 向上的动画
     */
    private Animation mRotateUpAnim;
    /**
     * 向下的动画
     */
    private Animation mRotateDownAnim;

    /**
     * 构造方法
     *
     * @param context context
     */
    public FooterLoadingLayout(Context context) {
        super(context);
        init(context);
    }

    /**
     * 构造方法
     *
     * @param context context
     * @param attrs   attrs
     */
    public FooterLoadingLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    /**
     * 初始化
     *
     * @param context context
     */
    private void init(Context context) {
        mProgressBar = (SimpleDraweeView) findViewById(R.id.pull_to_load_footer_progressbar);
//        mHintView = (TextView) findViewById(R.id.pull_to_load_footer_hint_textview);
        mArrowImageView = (ImageView) findViewById(R.id.pull_to_refresh_header_arrow);
        if (!isInEditMode())
            initLoadingPhoto();

//        setState(State.RESET);
//
        float pivotValue = 0.5f;    // SUPPRESS CHECKSTYLE
        float toDegree = -180f;     // SUPPRESS CHECKSTYLE
        // 初始化旋转动画
        mRotateUpAnim = new RotateAnimation(0.0f, toDegree, Animation.RELATIVE_TO_SELF, pivotValue,
                Animation.RELATIVE_TO_SELF, pivotValue);
        mRotateUpAnim.setDuration(ROTATE_ANIM_DURATION);
        mRotateUpAnim.setFillAfter(true);
        mRotateDownAnim = new RotateAnimation(toDegree, 0.0f, Animation.RELATIVE_TO_SELF, pivotValue,
                Animation.RELATIVE_TO_SELF, pivotValue);
        mRotateDownAnim.setDuration(ROTATE_ANIM_DURATION);
        mRotateDownAnim.setFillAfter(true);
    }

    private void initLoadingPhoto() {
        ImageRequest request = ImageRequestBuilder.newBuilderWithResourceId(R.drawable.pull_loading).build();
        DraweeController controller = Fresco.newDraweeControllerBuilder().setImageRequest(request).setAutoPlayAnimations(true).build();
        mProgressBar.setController(controller);
    }

    @Override
    protected View createLoadingView(Context context, AttributeSet attrs) {
        View container = LayoutInflater.from(context).inflate(R.layout.view_pull_to_load_footer, null);
        return container;
    }

    @Override
    public void setLastUpdatedLabel(CharSequence label) {
    }

    @Override
    public int getContentSize() {
        View view = findViewById(R.id.pull_to_load_footer_content);
        if (null != view) {
            return view.getHeight();
        }

        return (int) (getResources().getDisplayMetrics().density * 40);
    }

    @Override
    protected void onStateChanged(State curState, State oldState) {
//    	show(true);
//        mProgressBar.setVisibility(View.GONE);
//        mHintView.setVisibility(View.INVISIBLE);

        mArrowImageView.setVisibility(View.VISIBLE);
        mProgressBar.setVisibility(View.INVISIBLE);
        super.onStateChanged(curState, oldState);
    }

    @Override
    protected void onReset() {
//    	show(false);
        mArrowImageView.clearAnimation();
//        mHintView.setText(R.string.pull_to_refresh_header_hint_loading);
    }

    @Override
    protected void onPullToRefresh() {
//    	show(true);
        mArrowImageView.clearAnimation();
//        mHintView.setVisibility(View.VISIBLE);
//        mHintView.setText(R.string.pull_to_refresh_header_hint_normal2);
    }

    @Override
    protected void onReleaseToRefresh() {
//    	show(true);
        mArrowImageView.clearAnimation();
        mArrowImageView.startAnimation(mRotateUpAnim);
//        mHintView.setVisibility(View.VISIBLE);
//        mHintView.setText(R.string.pull_to_refresh_header_hint_ready);
    }

    @Override
    protected void onRefreshing() {
//    	show(true);
//        mHintView.setVisibility(View.VISIBLE);
//        mHintView.setText(R.string.pull_to_refresh_header_hint_loading);
        mArrowImageView.clearAnimation();
        mArrowImageView.setVisibility(View.INVISIBLE);
        mProgressBar.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onNoMoreData() {
//    	show(false);
//        mHintView.setText(R.string.pushmsg_center_no_more_msg);
    }

    @Override
    protected void onLoadingDrawableSet(Drawable imageDrawable) {

    }
}
